package git.soulbgm.ribbon.controller;

import git.soulbgm.ribbon.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 贺瑞杰
 * @version V1.0
 * @date 2018-09-28 14:47
 * @description
 */
@RestController
public class DemoController {

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("hello")
    public String hello(User user) {
        ServiceInstance producer = loadBalancerClient.choose("producer");
        String url = "http://" + producer.getHost() + ":" + producer.getPort() + "/hello?name=" + user.getName() + "&age=" + user.getAge();
        System.out.println(url);
        return restTemplate.getForObject(url, String.class);
    }
}
